//
//  XMJigsawShareController.m
//  XiaoMiao
//
//  Created by 23 on 2018/2/24.
//  Copyright © 2018年 smallyou. All rights reserved.
//

#import "XMJigsawShareController.h"
#import "OpenShareHeader.h"
#import "XMAlbumController.h"

typedef NS_OPTIONS(NSUInteger, KLSharePlatformType) {
    KLSharePlatformTypeWeixin           = 1 << 0,
    KLSharePlatformTypeQQ               = 1 << 1,
    KLSharePlatformTypeQQZone           = 1 << 2,
    KLSharePlatformTypeWeixinFriend     = 1 << 3,
};

@interface XMJigsawShareController ()

@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *iconHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *iconWidth;
@property (weak, nonatomic) IBOutlet UIButton *backButton;

@end

@implementation XMJigsawShareController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"保存分享";
    
    //将图片保存到本地相册
    UIImageWriteToSavedPhotosAlbum(self.snapImage, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    
    
    self.iconImageView.image = self.snapImage;
    
    self.iconWidth.constant = (self.snapImage.size.width*0.7);
    self.iconHeight.constant = (self.snapImage.size.height*0.7);
    
    self.backButton.layer.borderColor = kMainBlueColor.CGColor;
    self.backButton.layer.borderWidth = 1;
    self.backButton.layer.cornerRadius = self.backButton.height/2;
    self.backButton.clipsToBounds = YES;

}

-(void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    if(error)
    {
        [KLProgressHUD showPromptHUDWithMessage:@"保存图片失败，请重试"];
        return;
    }
    
    [KLProgressHUD showSuccessHUDWithMessage:@"保存图片成功"];
}


void (^Callback)(UIImage *image, KLSharePlatformType type) = ^(UIImage *image, KLSharePlatformType type){
    
    //创建分享内容
    NSMutableDictionary *message = [NSMutableDictionary dictionary];
    message[@"title"] = @"术前术后";
    message[@"image"] = image;
//    message[@"thumbnail"] = image;
    message[@"desc"] = @"术前术后"; //内容前40个字  -- 可能没有40个字
    
    
    OSMessage *msg = [[OSMessage alloc]init];
    msg.title =  message[@"title"];
    msg.image = message[@"image"];
    msg.thumbnail = message[@"thumbnail"];
    msg.desc = message[@"desc"];
    
    
    if (type == KLSharePlatformTypeWeixin) {
        //分享纯文本消息到微信
        if (![OpenShare isWeixinInstalled]) {
            NSLog(@"未安装微信");
            [KLProgressHUD showErrorHUDWithMessage:@"未安装微信"];
            return;
        }
        [OpenShare shareToWeixinSession:msg Success:^(OSMessage *message) {
            NSLog(@"微信分享成功：\n%@",message);
        } Fail:^(OSMessage *message, NSError *error) {
            NSLog(@"微信分享失败：\n%@",message);
        }];
    }else if (type == KLSharePlatformTypeWeixinFriend){
        if (![OpenShare isWeixinInstalled]) {
            NSLog(@"未安装微信");
            [KLProgressHUD showErrorHUDWithMessage:@"未安装微信"];
            return;
        }
        [OpenShare shareToWeixinTimeline:msg Success:^(OSMessage *message){
            NSLog(@"分享朋友圈成功：\n%@",message);
        }
                                    Fail:^(OSMessage *message, NSError *error) {
                                        NSLog(@"分享朋友圈失败：\n%@",message);
                                    }];
    }else if (type == KLSharePlatformTypeQQ){
        if (![OpenShare isQQInstalled]) {
            NSLog(@"未安装QQ");
            [KLProgressHUD showErrorHUDWithMessage:@"未安装QQ"];
            return;
        }
        [OpenShare shareToQQFriends:msg Success:^(OSMessage *message) {
            //                NSLog(@"QQ分享成功：\n%@",message);
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"分享成功" message:@"" delegate:nil cancelButtonTitle:@"" otherButtonTitles:nil];
            [alert show];
        } Fail:^(OSMessage *message, NSError *error) {
            NSLog(@"QQ分享失败：\n%@\n%@",error,message);
        }];
    }else if (type == KLSharePlatformTypeQQZone){
        if (![OpenShare isQQInstalled]) {
            NSLog(@"未安装QQ");
            [KLProgressHUD showErrorHUDWithMessage:@"未安装QQ"];
            return;
        }
        
        [OpenShare shareToQQZone:msg Success:^(OSMessage *message) {
            
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"分享成功" message:@"" delegate:nil cancelButtonTitle:@"" otherButtonTitles:nil];
            [alert show];
            
        } Fail:^(OSMessage *message, NSError *error) {
            
        }];
    }
    
};


- (IBAction)actionBackAlbum:(id)sender {
    
    for (UIViewController *vc in self.navigationController.childViewControllers) {
        if ([vc isKindOfClass:[XMAlbumController class]]) {
            [self.navigationController popToViewController:vc animated:YES];
            break;
        }
    }
    
}

- (IBAction)actionFriend:(id)sender {
    
    Callback(self.snapImage, KLSharePlatformTypeWeixinFriend);
    
}
- (IBAction)actionWechat:(id)sender {
    Callback(self.snapImage, KLSharePlatformTypeWeixin);
}
- (IBAction)actionQQ:(id)sender {
    Callback(self.snapImage, KLSharePlatformTypeQQ);
}
- (IBAction)actionZone:(id)sender {
    Callback(self.snapImage, KLSharePlatformTypeQQZone);
}

@end
